<?php
// +----------------------------------------------------------------------
// | ThinkPHP [ WE CAN DO IT JUST THINK ]
// +----------------------------------------------------------------------
// | Copyright (c) 2006-2016 http://thinkphp.cn All rights reserved.
// +----------------------------------------------------------------------
// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
// +----------------------------------------------------------------------
// | Author: 流年 <liu21st@gmail.com>
// +----------------------------------------------------------------------

// 应用公共文件

/**
 * 接口返回成功
 * @param array $data
 * @param string $msg
 * @return \think\response\Json
 */
function success($data = [], $msg = '')
{
    $response = array(
        'code' => 0,
        'message' => $msg,
        'data' => $data,
    );
    return json($response, 200, ['Access-Control-Allow-Origin' => '*',
        'Access-Control-Allow-Headers' => 'x-requested-with,content-type',
        'Access-Control-Allow-Methods' => 'POST,GET,OPTIONS,DELETE']);
}

/**
 * 分页格式化
 * @param string $name 数据名
 * @param array|\think\Paginator $pageData
 * @return array
 */
function formatPage($name, $pageData)
{
    if (!is_array($pageData)) {
        $pageData = $pageData->toArray();
    }
    $pages = $pageData['last_page'];
    $data = $pageData['data'];
    return [$name => $data, 'pages' => $pages];
}

/**
 * 接口返回失败
 * @param string $msg
 * @param array $data
 * @return \think\response\Json
 */
function error($msg = '', $data = [])
{
    $response = array(
        'code' => 1,
        'message' => $msg,
        'data' => $data,
    );
    return json($response, 200, ['Access-Control-Allow-Origin' => '*',
        'Access-Control-Allow-Headers' => 'x-requested-with,content-type',
        'Access-Control-Allow-Methods' => 'POST,GET,OPTIONS,DELETE']);
}
